/************************************************************************************/
/*  Program name CAPFAEXT_BOOK_CTL.sql                                              */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
SET VERIFY OFF

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAEXT_BOOK_CTL';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPFA_BOOK_CTL.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO_Name		VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_F(p_NGO VARCHAR2) IS
	SELECT
		*
	FROM
		FA_BOOK_CONTROLS
	WHERE	SUBSTR(Book_Type_Code,1,4)	=	p_NGO;

BEGIN
	lv_NGO_Name		:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO_Name;

	IF	lv_NGO_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_NGO_Name) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Book_Type_Code||CHR(9)||
				C1.Book_Type_Name||CHR(9)||
					C1.Set_Of_Books_ID||CHR(9)||
				C1.Initial_Date||CHR(9)||
				C1.Last_Deprn_Run_Date||CHR(9)||
				C1.Amortize_Flag||CHR(9)||
				C1.Fully_Reserved_Flag||CHR(9)||
				C1.Deprn_Calendar||CHR(9)||
				C1.Book_Class||CHR(9)||
				C1.GL_Posting_Allowed_Flag||CHR(9)||
				C1.Current_Fiscal_Year||CHR(9)||
				C1.Allow_Mass_Changes||CHR(9)||
				C1.Allow_Deprn_Adjustments||CHR(9)||
				C1.Accounting_Flex_Structure||CHR(9)||
				C1.Last_Update_Date||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
				C1.Prorate_Calendar||CHR(9)||
				C1.Date_Ineffective||CHR(9)||
				C1.JE_Retirement_Category||CHR(9)||
				C1.JE_Depreciation_Category||CHR(9)||
				C1.JE_Reclass_Category||CHR(9)||
				C1.GL_JE_Source||CHR(9)||
				C1.JE_Addition_Category||CHR(9)||
				C1.JE_Adjustment_Category||CHR(9)||
				C1.Distribution_Source_Book||CHR(9)||
				C1.JE_Transfer_Category||CHR(9)||
				C1.Copy_Retirements_Flag||CHR(9)||
				C1.Copy_Adjustments_Flag||CHR(9)||
				C1.Deprn_Request_ID||CHR(9)||
				C1.Allow_Cost_Ceiling||CHR(9)||
				C1.Allow_Deprn_Exp_Ceiling||CHR(9)||
				C1.Calculate_NBV||CHR(9)||
				C1.Run_Year_End_Program||CHR(9)||
				C1.JE_Deferred_Deprn_Category||CHR(9)||
				C1.Allow_CIP_Assets_Flag||CHR(9)||
				C1.ITC_Allowed_Flag||CHR(9)||
					C1.Created_By||CHR(9)||
				C1.Creation_Date||CHR(9)||
				C1.Last_Update_Login||CHR(9)||
				C1.Allow_Mass_Copy||CHR(9)||
				C1.Allow_Purge_Flag||CHR(9)||
				C1.Allow_Reval_Flag||CHR(9)||
				C1.Amortize_Reval_Reserve_Flag||CHR(9)||
				C1.AP_Intercompany_Acct||CHR(9)||
				C1.AR_Intercompany_Acct||CHR(9)||
				C1.Attribute1||CHR(9)||
				C1.Attribute2||CHR(9)||
				C1.Attribute3||CHR(9)||
				C1.Attribute4||CHR(9)||
				C1.Attribute5||CHR(9)||
				C1.Attribute6||CHR(9)||
				C1.Attribute7||CHR(9)||
				C1.Attribute8||CHR(9)||
				C1.Attribute9||CHR(9)||
				C1.Attribute10||CHR(9)||
				C1.Attribute11||CHR(9)||
				C1.Attribute12||CHR(9)||
				C1.Attribute13||CHR(9)||
				C1.Attribute14||CHR(9)||
				C1.Attribute15||CHR(9)||
				C1.Attribute_Category_Code||CHR(9)||
				C1.Capital_Gain_Threshold||CHR(9)||
				C1.Copy_Salvage_Value_Flag||CHR(9)||
				C1.Cost_Of_Removal_Clearing_Acct||CHR(9)||
				C1.Cost_Of_Removal_Gain_Acct||CHR(9)||
				C1.Cost_Of_Removal_Loss_Acct||CHR(9)||
				C1.Default_Life_Extension_Ceiling||CHR(9)||
				C1.Default_Life_Extension_Factor||CHR(9)||
				C1.Default_Max_Fully_Rsvd_Revals||CHR(9)||
				C1.Default_Reval_Fully_Rsvd_Flag||CHR(9)||
				C1.Deferred_Deprn_Expense_Acct||CHR(9)||
				C1.Deferred_Deprn_Reserve_Acct||CHR(9)||
				C1.Deprn_Allocation_Code||CHR(9)||
				C1.Deprn_Status||CHR(9)||
				C1.Fiscal_Year_Name||CHR(9)||
				C1.Initial_Period_Counter||CHR(9)||
				C1.JE_CIP_Adjustment_Category||CHR(9)||
				C1.JE_CIP_Addition_Category||CHR(9)||
				C1.JE_CIP_Reclass_Category||CHR(9)||
				C1.JE_CIP_Retirement_Category||CHR(9)||
				C1.JE_CIP_Reval_Category||CHR(9)||
				C1.JE_CIP_Transfer_Category||CHR(9)||
				C1.JE_Reval_Category||CHR(9)||
				C1.Last_Mass_Copy_Period_Counter||CHR(9)||
				C1.Last_Period_Counter||CHR(9)||
				C1.Last_Purge_Period_Counter||CHR(9)||
				C1.Mass_Copy_Source_Book||CHR(9)||
				C1.Mass_Request_ID||CHR(9)||
				C1.NBV_Amount_Threshold||CHR(9)||
				C1.NBV_Fraction_Threshold||CHR(9)||
				C1.NBV_Retired_Gain_Acct||CHR(9)||
				C1.NBV_Retired_Loss_Acct||CHR(9)||
				C1.Proceeds_Of_Sale_Clearing_Acct||CHR(9)||
				C1.Proceeds_Of_Sale_Gain_Acct||CHR(9)||
				C1.Proceeds_Of_Sale_Loss_Acct||CHR(9)||
				C1.Revalue_On_Retirement_Flag||CHR(9)||
				C1.Reval_Deprn_Reserve_Flag||CHR(9)||
				C1.Reval_Posting_Flag||CHR(9)||
				C1.Reval_RSV_Retired_Gain_Acct||CHR(9)||
				C1.Reval_RSV_Retired_Loss_Acct||CHR(9)||
				C1.Deprn_Adjustment_Acct||CHR(9)||
				C1.Immediate_Copy_Flag||CHR(9)||
				C1.JE_Deprn_Adjustment_Category||CHR(9)||
				C1.Depr_First_Year_Ret_Flag||CHR(9)||
					C1.Flexbuilder_Defaults_CCID||CHR(9)||
				C1.Retire_Reval_Reserve_Flag||CHR(9)||
				C1.Use_Current_NBV_For_Deprn||CHR(9)||
				C1.Copy_Additions_Flag||CHR(9)||
				C1.Use_Percent_Salvage_Value_Flag||CHR(9)||
				C1.MC_Source_Flag||CHR(9)||
				C1.Reval_YTD_Deprn_Flag||CHR(9)||
				C1.Global_Attribute1||CHR(9)||
				C1.Global_Attribute2||CHR(9)||
				C1.Global_Attribute3||CHR(9)||
				C1.Global_Attribute4||CHR(9)||
				C1.Global_Attribute5||CHR(9)||
				C1.Global_Attribute6||CHR(9)||
				C1.Global_Attribute7||CHR(9)||
				C1.Global_Attribute8||CHR(9)||
				C1.Global_Attribute9||CHR(9)||
				C1.Global_Attribute10||CHR(9)||
				C1.Global_Attribute11||CHR(9)||
				C1.Global_Attribute12||CHR(9)||
				C1.Global_Attribute13||CHR(9)||
				C1.Global_Attribute14||CHR(9)||
				C1.Global_Attribute15||CHR(9)||
				C1.Global_Attribute16||CHR(9)||
				C1.Global_Attribute17||CHR(9)||
				C1.Global_Attribute18||CHR(9)||
				C1.Global_Attribute19||CHR(9)||
				C1.Global_Attribute20||CHR(9)||
				C1.Global_Attribute_Category||CHR(9)||
					C1.Org_ID||CHR(9)||
				CAPBREXT_COMMON.GetSOBShortName(C1.Set_Of_Books_ID)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Created_By)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Flexbuilder_Defaults_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetOrgName(C1.Org_ID)
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO_Name||': No such NGO exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/